// ==UserScript==
// @name           csair_showflights
// @namespace      csair
// @include        http://www.csair.com/cn/index.asp
// ==/UserScript==

var departure='SHA'; // shanghai
var destination='SYX'; //sanya
var whendoyouleave='2009-03-23';  //date
var adultCount=3;
var childCount=2;
var delay = 5; // in seconds

function selectOptionInOptions(tagSelect, val)
{
	var opt;
	for(var j = 0; j < tagSelect.childNodes.length; j++) // options
	{
			opt = tagSelect.childNodes[j];
			
			if(opt.value && (opt.value == val) )
			{
				opt.selected = true;
				return 0;
			}
	}
	return -1;
}


function selectOptionInOptionGroups(tagSelect, val)
{
	var optgroup;
	var opt;
	for(var i = 0; i < tagSelect.childNodes.length; i++) // optgroups
	{
		optgroup = tagSelect.childNodes[i];
		for(var j = 0; j < optgroup.childNodes.length; j++) // options
		{
				opt = optgroup.childNodes[j];
				if(opt.value && (opt.value == val) )
				{
					opt.selected = true;
					return 0;
				}
		}
	}
	
	return -1;
}


function fillData(from, to, when, adult, child)
{
	
	// fill departure
	var tagFromSelects = document.evaluate(
			"//SELECT[@id='city1']",
			document,
			null,
			XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
			null);
		
		if(tagFromSelects.snapshotLength)
		{
			var tagFromSelect = tagFromSelects.snapshotItem(0);

			if(selectOptionInOptionGroups(tagFromSelect, from) != 0)
			{
				alert("can not find your departure, please check it!");
				return;
			}
		}
		
		// fill destination
		var tagDestSelects = document.evaluate(
			"//SELECT[@id='city2']",
			document,
			null,
			XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
			null);
		
		if(tagDestSelects.snapshotLength)
		{
			var tagDestSelect = tagDestSelects.snapshotItem(0);

			if(selectOptionInOptionGroups(tagDestSelect, to) != 0)
			{
				alert("can not find your destination, please check it!");
				return;
			}
		}
	
	
	
	// fill adult
	var tagAdultSelects = document.evaluate(
			"//SELECT[@id='adult']",
			document,
			null,
			XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
			null);
	
	if(tagAdultSelects.snapshotLength)
	{
		var tagAdultSelect = tagAdultSelects.snapshotItem(0);

		if(selectOptionInOptions(tagAdultSelect, adult) != 0)
		{
			alert("adult number error, please check it!");
			return;
		}
	}
	
	// fill childcount
	var tagChildSelects = document.evaluate(
			"//SELECT[@id='child']",
			document,
			null,
			XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
			null);
	
	if(tagChildSelects.snapshotLength)
	{
		var tagChildSelect = tagChildSelects.snapshotItem(0);

		if(selectOptionInOptions(tagChildSelect, child) != 0)
		{
			alert("child number error, please check it!");
			return;
		}
	}
	
	
	// fill time
	var tagDateTexts = document.evaluate(
			"//INPUT[@id='date1']",
			document,
			null,
			XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
			null);
	
	if(tagDateTexts.snapshotLength)
	{
		var tagDateText = tagDateTexts.snapshotItem(0);
		tagDateText.readonly=false;
		tagDateText.value = when;
	}
}



function postData()
{
	var inputs = document.evaluate(
	   "//INPUT[@id='fltsmtbtn']",
	    document,
			null,
			XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
			null);
	if(inputs.snapshotLength)
	{
		var _input = inputs.snapshotItem(0);
		_input.click();
	}
	
}


function run()
{
	fillData(departure, destination, whendoyouleave, adultCount, childCount);
	postData();
}



// auto submit date after some seconds
setTimeout(run, delay*1000);